package com.xej.seat.controller.admin;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.xej.seat.service.CustomerService;
import com.xej.seat.tag.LevelCustomerTag;
import com.xej.seat.tag.Tag;
import com.xej.seat.util.ResultInfo;
import com.xej.seat.util.ResultInfoFactory;
import com.xej.seat.util.excel.ExportExcel;
import com.xej.seat.util.excel.ImportExcel;
import com.xej.seat.vo.Customer;
import com.xej.seat.vo.Pager;

/**
 * 后台管理 参会人员controller
 */
@Controller
@RequestMapping("/admin/customer")
public class AdminCustomerController {
    @Resource
    private CustomerService customerService;
    
    @Resource(type = LevelCustomerTag.class)
	private Tag tag;
    
    @RequestMapping("/initPage")
    @ResponseBody
    public Pager initPage(Pager pager){
    	customerService.initPage(pager);
        return pager;
    }
    
    
    /**
     * 加载参会人员信息列表
     * @param pager 分页对象
     * @param categoryName  搜索条件
     * @param model
     * @return
     */
    @RequestMapping("/load")
    public String loadCoustomer(Pager<Customer> pager ,String cusName,Model model){
        List<Customer> customerList = customerService.loadCustomerList(pager,cusName);
        model.addAttribute("customerList",customerList);
        return "admin/customer/customerTable";
    }
    
    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/addCustomer")
    public String addPage() {
        return "admin/customer/customerAdd";
    }
    
    @RequestMapping("/initCustomer")
    @ResponseBody
    public ResultInfo initCustomer(Customer  customer, HttpServletRequest request){
    	List<Customer> list = new LinkedList<Customer>();
    	list.add(customer);
    	if(!CollectionUtils.isEmpty(list)) {
    		customerService.initCustomer(list);
    		 return ResultInfoFactory.getSuccessResultInfo("创建客户成功！！！");
    	}
    	 return ResultInfoFactory.getSuccessResultInfo("创建客户失败！！！");
    }
    
    /**
     * 跳转到导入页面
     * @return
     */
    @RequestMapping("/toImportCustomerPage")
    public String toImportCustomerPage() {
        return "admin/customer/customerImport";
    }
    
    @RequestMapping(value = "importCustomer", method=RequestMethod.POST)
    @ResponseBody
    public ResultInfo importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			ImportExcel ei = new ImportExcel(file, 0, 0);
			List<Customer> list = ei.getDataList(Customer.class);
			if(list != null && list.size() > 0) {
				for(Customer cus : list) {
					//计算客户权重
					double cweight = this.tag.getWeight(cus);
					cus.setCweight(cweight);
				}
				customerService.initCustomer(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultInfoFactory.getSuccessResultInfo("导入成功！！！");
    }
    
    /**
	 * 下载导入客户数据模板
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
    @RequestMapping(value = "import/template")
    @ResponseBody
    public ResultInfo importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "客户数据导入模板.xlsx";
    		List<Customer> list = Lists.newArrayList(); 
    		new ExportExcel("客户数据", Customer.class, 2).setDataList(list).write(response, fileName).dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultInfoFactory.getSuccessResultInfo("下载成功！！！");
    }
}